package com.stylefeng.guns.modular.purchase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.dto.PurchaseAddRequest;
import com.stylefeng.guns.modular.dto.PurchaseQueryRequest;
import com.stylefeng.guns.modular.manager.service.IManagerGoodsService;
import com.stylefeng.guns.modular.purchase.service.IPurchaseGoodsService;
import com.stylefeng.guns.modular.purchase.service.IPurchaseService;
import com.stylefeng.guns.modular.system.dao.PurchaseMapper;
import com.stylefeng.guns.modular.system.model.ManagerGoods;
import com.stylefeng.guns.modular.system.model.Purchase;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuXin
 * @since 2018-09-12
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements IPurchaseService {

    @Autowired
    private IPurchaseGoodsService purchaseGoodsService;
    /**
     * 库存服务
     */
    @Autowired
    private IManagerGoodsService managerGoodsService;

    @Override
    public Page<Purchase> selectPageWithDataScope(Page<Purchase> page,PurchaseQueryRequest request) {
        EntityWrapper<Purchase> entityWrapper = new EntityWrapper<>();
        //订单号
        if(StringUtils.isNotBlank(request.getOrderNumber())){
            entityWrapper.eq("order_number", request.getOrderNumber());
        }

        //是否支付
        if(StringUtils.isNotBlank(request.getState())){
            entityWrapper.eq("state", Integer.valueOf(request.getState()));
        }

        //开始日期 大于等于
        if(StringUtils.isNotBlank(request.getbDate())){
            entityWrapper.ge("create_date", DateUtil.parse(request.getbDate(),"yyyy-MM-dd"));
        }

        //结束日期 小于等于
        if(StringUtils.isNotBlank(request.geteDate())){
            entityWrapper.le("create_date", DateUtil.parse(request.geteDate(),"yyyy-MM-dd"));
        }

        //根据备注信息查询
        if(StringUtils.isNotBlank(request.getRemarks())){
            entityWrapper.like("remarks", request.getRemarks());
        }
        if(ShiroKit.isAdmin()){
            Page<Purchase> result = this.selectPage(page, entityWrapper);
            return result;
        }else {
            DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
            List<Purchase> result = this.baseMapper.selectPageWithDataScope(page,dataScope,request);
            page.setRecords(result);
            return page;
        }
    }

    @Override
    public boolean insertListAndGoods(PurchaseAddRequest request) {
        Purchase purchase = new Purchase();
        BeanUtils.copyProperties(request, purchase);
        boolean success = this.insert(purchase);

        //先取出商品id和本次新增的商品数量
        Map<Long, Integer> tempMap = Maps.newHashMap();
        request.getPurchaseGoodsList().forEach(purchaseGoods -> {
            purchaseGoods.setPurchaseId(purchase.getId());
            tempMap.put(purchaseGoods.getGoodsId(), purchaseGoods.getGoodsNum());
        });
        //更新库存数量
        managerGoodsService.batchAddGoodsInventory(tempMap);
        boolean insertPurchaseListSuccess = purchaseGoodsService.insertBatch(request.getPurchaseGoodsList());
        return success && insertPurchaseListSuccess;
    }
}
